﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Sector7.Restaurant.EntityType;
using Sector7.Framework.Core;
using System.Data.SqlClient;
using Sector7.Restaurant.DAO.Utility;
using Sector7.Framework.DB;

namespace Sector7.Restaurant.DAO.OleDB
{
    public class MenuItemLabelDAO : IMenuItemLabelDAO
    {
        public int Create(RS_MenuItemLabel label)
        {
            using (DBHelper opt = new DBHelper())
            {
                string sql = 
                    @"INSERT INTO RS_MenuItemLabel
                               (LabelName
                               ,Image)
                         VALUES
                               (@LabelName
                               ,@Image)
                    SELECT @@IDENTITY";

                return Convert.ToInt32(opt.ExecuteScalar(sql,
                    DBUtil.CreateSqlParameter(label.LabelName, "LabelName"),
                    DBUtil.CreateSqlParameter(label.Image, "Image")));
            }
        }

        public List<RS_MenuItemLabel> ReadAll()
        {
            List<RS_MenuItemLabel> labels = new List<RS_MenuItemLabel>();

            using (DBHelper opt = new DBHelper())
            {
                string sql =
                    @"SELECT *
                        FROM RS_MenuItemLabel";

                SqlDataReader reader = opt.ExecuteQuery(sql);

                while (reader.Read())
                {
                    object label = new RS_MenuItemLabel();

                    DBUtil.ReadData(reader, ref label);

                    labels.Add(label as RS_MenuItemLabel);
                }
            }

            return labels;
        }

        public List<RS_MenuItemLabel> ReadAll(int labelIds)
        {
            List<RS_MenuItemLabel> labels = new List<RS_MenuItemLabel>();

            if (labelIds != null || labelIds > 0)
            {
                using (DBHelper opt = new DBHelper())
                {
                    string sql = @"
                        SELECT *
                          FROM RS_MenuItemLabel
                          WHERE (LabelId | @LabelIds) = @LabelIds";

                    SqlDataReader reader = opt.ExecuteQuery(sql,
                        DBUtil.CreateSqlParameter(labelIds, "LabelIds"));

                    while (reader.Read())
                    {
                        object label = new RS_MenuItemLabel();

                        DBUtil.ReadData(reader, ref label);

                        labels.Add(label as RS_MenuItemLabel);
                    }
                }
            }

            return labels;
        }

        public int Update(EntityDelta delta)
        {
            using (DBHelper opt = new DBHelper())
            {
                return opt.ExecuteNonQuery(
                    DBUtil.CreateUpdateSql(delta),
                    DBUtil.CreateUpdateParameters(delta));
            }
        }
    }
}
